<template>
  <gj-loadmore :bottom-method="loadBottom" ref="loadmore">
    <table class="table">
      <tbody class="rockmans">
      <tr v-for="(item,index) in rockmans.list" id="item.userCode" :key="index">
        <td class="one">
          <a href="javascript:;" @click.stop.prevent="toDetails(item)">
            <img :src="item.userHead" />
            <div class="inline-block">
              <div class="nickname">{{item.nickname}}</div>
              <div class="value" :class="item.totalRateClass">{{item.totalRate}}</div>
              <div class="key">总盈利率</div>
            </div>
          </a>
        </td>
        <td class="two">
          <div>
            <div class="clearfix">
              <div class="frequency">
                <div class="value">{{item.opCountMonth}}</div>
                <div class="key">操作频率/月</div>
              </div>
              <div class="price">
                <div class="value">{{item.subscribePrice}}</div>
                <div class="key">订阅价</div>
              </div>
            </div>
            <div class="push" @click.stop.prevent="setPush(item,index)">
              <i class="iconfont icon-shixinduigou" :class="item.pushFlagClass"></i>
              <span>给朕发推送信息</span>
            </div>
          </div>
        </td>
        <td class="three">
          <a href="javascript:;" data-toggle="modal" data-relation-user-code="item.userCode" @click.stop.prevent="cancelSubscribe(item,index)">
            <i class="iconfont icon-qingkong1"></i>
            <div>取消订阅</div>
          </a>
        </td>
      </tr>
      </tbody>
    </table>
  </gj-loadmore>
</template>

<script>
  import CommonUtil from 'common/common.util'
  import LocalDAO from 'common/localDAO'
  import RemoteDAO from 'common/remoteDAO'
  import Constant from 'common/constant'
  import LoadMore from 'components/LoadMore'
  export default {
    data () {
      return {
        userCode: LocalDAO.getUserCode(),
        rockmans: {
          list: []
        },
        pageSize: 20,
        pageNo: 1
      }
    },
    components:{
      'gj-loadmore':LoadMore
    },
    methods: {
      //上拉加载
      loadBottom () {
        this.getData();
      },

      //获取我订阅的牛人列表
      getData () {
        RemoteDAO.callUC12008301(this.userCode, "1", this.pageSize, this.pageNo, "0", response => {
          if(response.error == 0){
            if(response.data.body.length > 0){
              let list = [];
              let targetUserCodes = [];
              response.data.body.forEach((item,index,array) => {
                let object = {};
                Object.assign(object,{
                  userCode: item.USER_CODE,
                  nickname: item.NICKNAME,
                  userHead: item.USER_HEAD == "" ? CommonUtil.getDefaultUserHead(item.USER_CODE) : item.USER_HEAD,
                  totalRate: '--%',
                  totalRateClass: 'positive',
                  opCountMonth: item.OP_COUNT_MONTH,
                  pushFlag: item.PUSH_FLAG,
                  pushFlagClass: item.PUSH_FLAG == '1' ? 'orange' : '',
                  subscribeFlag: item.SUBSCRIBE_FLAG,
                  subscribePrice: item.SUBSCRIBE_PRICE == '' ? '0' : CommonUtil.toFixed(item.SUBSCRIBE_PRICE, 2)
                });
                targetUserCodes.push(object.userCode);
                list.push(object);
              })
              RemoteDAO.callJCL30032(targetUserCodes.join(","), response => {
                if (response.error == 0) {
                  if(response.data.body.data.length > 0){
                    response.data.body.data.forEach((target,index,array) => {
                      let targetUserCode = target.client_id;
                      list.forEach((item,index,array) => {
                        if(item.userCode == targetUserCode){
                          let totalRate = CommonUtil.toFixed(target.total_floatprofit, 2);
                          if(totalRate >=0){
                            if(totalRate.indexOf('+') != 0){
                              totalRate = '+' + totalRate;
                            }
                          }else{
                            if(totalRate.indexOf('-') != 0){
                              totalRate = '-' + totalRate;
                            }
                          }
                          item.totalRate = totalRate + "%";
                          item.totalRateClass = item.totalRate.indexOf('-') >= 0 ? 'negative': 'positive'
                        }
                      })
                    })
                    this.rockmans.list = this.rockmans.list.concat(list);
                    // 更新分享页数据
                    var shareSubscriptions = LocalDAO.getShareSubscriptions();
                    if(shareSubscriptions == null){
                      shareSubscriptions = {};
                      shareSubscriptions.rockmans = {};
                      shareSubscriptions.rockmans.list = [];
                    }else if(typeof(shareSubscriptions.rockmans.list) == "undefined"){
                      shareSubscriptions.rockmans.list = [];
                    }
                    shareSubscriptions.rockmans.list = this.rockmans.list;
                    LocalDAO.setShareSubscriptions(shareSubscriptions);
                    //上拉加载下拉刷新
                    if(list.length == this.pageSize){
                      this.pageNo = this.pageNo + 1;
                      this.$refs.loadmore.onBottomLoaded(); //加载成功
                    }else{
                      this.$refs.loadmore.onBottomAllLoaded(); //无更多数据
                    }
                  }else{
                    if(this.pageNo == 1){
                      this.$refs.loadmore.onBottomError("暂无数据");//暂无数据
                    }else{
                      this.$refs.loadmore.onBottomAllLoaded(); //无更多数据
                    }
                  }
                } else {
                  this.$refs.loadmore.onBottomError(response.data);//加载失败
                }
              })
            }else{
              if(this.pageNo == 1){
                this.$refs.loadmore.onBottomError("暂无数据");//暂无数据
              }else{
                this.$refs.loadmore.onBottomAllLoaded(); //无更多数据
              }
            }
          }else{
            this.$refs.loadmore.onBottomError(response.data);//加载失败
          }
        })
      },

      //设置消息推送
      setPush (item,index) {
        var relationUserCode = item.userCode;
        var relationUserName = item.nickname;
        var pushFlag = item.pushFlag;
        var actionType = "";
        if (pushFlag == "1") {
          actionType = "02"; // 取消推送
        } else {
          actionType = "00"; // 增加推送
        }
        var args = {};
        args.userCode = this.userCode;
        args.actionType = actionType;
        args.businType = "NRNG";
        args.communityType = "5";
        args.objectType = "1";
        args.relationUserCode = relationUserCode;
        args.relationUserName = relationUserName;
        args.beginDate = "";
        args.endDate = "";
        args.communityContent = "";
        args.topicCode = "" ;
        args.topicName = "";
        args.serialNo = "";
        RemoteDAO.callUC12008306(args, response => {
          if (response.error == "0") {
            if(pushFlag == "1"){
              item.pushFlag = "0";
              item.pushFlagClass = '';
            }else{
              item.pushFlag = "1";
              item.pushFlagClass = 'orange';
            }
            this.rockmans.list.splice(index,1,item);
            this.$store.dispatch('setTip',{text:'操作成功'});
          }else{
            this.$store.dispatch('setTip',{text:response.data});
          }
        })
      },

      //点击牛人头像跳转至牛人详情页面
      toDetails (item) {
        this.$router.push({path:'/otherdetail',query:{userCode:item.userCode}});
      },

      //取消订阅
      cancelSubscribe (item,index) {
        this.$store.dispatch('setModal', {visible: true, modalId: 'subscribeCancelModal',relationUserCode:item.userCode,callback:this.callback});
      },

      //取消订阅回调函数
      callback (response) {
        if(response.error == 0){
          for(let i=0;i<this.rockmans.list.length;i++){
            let rockman = this.rockmans.list[i];
            if(rockman.userCode == this.$store.state.modal.relationUserCode){
              this.rockmans.list.splice(i,1);
              break;
            }
          }
        }
      }
    }
  }
</script>
